//index.js
//获取应用实例
const app = getApp()
 
Page({
  data: {
    username: '',
    password: '',
    img: '',
    code: 1525,
    uuid: ""
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onShow: function () {
    // 生命周期函数--监听页面显示
    // wx.hideTabBar({})
  },
  onLoad: function () {
    var that = this
    var user = wx.getStorageSync('user');
    if (user){
      wx.reLaunch({
        url: "/pages/index/index"
      })
    }
    wx.request({
      url: 'http://localhost:8080/captchaImage', // 登录接口
      method: 'GET',
      header: {
        'content-type': 'application/json'  //这里注意POST请求content-type是小写，大写会报错  
      },
      success(res) {
        if (res.data.code) {
          that.setData({
            img:  "data:image/gif;base64," + res.data.img,
            uuid: res.data.uuid
          })
        } else {
          wx.showToast({
            title: "初始化验证码失败",
            icon: 'none',
          })
        }
      }
    })
  },
 
 
  // 获取输入账号
  usernameInput: function (e) {
    this.setData({
      username: e.detail.value
    })
  },
 
  // 获取输入密码 
  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
 
  // 获取输入验证码
  codeInput: function (e) {
    this.setData({
      code: e.detail.value
    })
  },

  // 去注册页面
  goToRegister: function () {
    wx.reLaunch({
      url: "/pages/register/index",
    })
  },
 
  // 登录处理
  handleGetUserInfo(e){
    var that = this;
    if (this.data.username.length == 0 || this.data.password.length == 0
      || this.data.code.length == 0) {
      wx.showToast({
        title: '账号或密码不能为空',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.request({
        url: 'http://localhost:8080/login', // 登录接口
        method: 'post',
        header: {
          'content-type': 'application/json'  //这里注意POST请求content-type是小写，大写会报错  
        },
        data: {
          username: that.data.username,
          password: that.data.password,
          code: that.data.code,
          uuid: that.data.uuid
        },
        success(res) {
          if(res.data.code==200){
            wx.setStorageSync('token', res.data.token)
            wx.setStorageSync('username', that.data.username)
            wx.request({
              url: 'http://127.0.0.1:8080/getInfo', // 查询用户信息
              method: 'GET',
              header: {
                'content-type': 'application/json',  //这里注意POST请求content-type是小写，大写会报错  
                'Authorization': res.data.token
              },
              success(res) {
                if (res.data.code==200) {
                  wx.setStorageSync('user', res.data.user)
                  wx.reLaunch({
                    url: "/pages/index/index"
                  })
                } else {
                  wx.showToast({
                    title: "token失效",
                    icon: 'none',
                  })
                }
              }
            })
          }else{
            wx.showToast({
              title: '账号或密码错误',
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  }
})